package com.bugbuff.dubbo.server.service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bugbuff.common.mapper.HoutaiMapper;
import com.bugbuff.common.mapper.UserMapper;
import com.bugbuff.common.pojo.User;
import com.bugbuff.dubbo.server.api.HoutaiApi;
import com.bugbuff.dubbo.server.pojo.Logs;
import com.bugbuff.dubbo.server.vo.SummaryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class HoutaiService {

    @Reference
    private HoutaiApi houtaiApi;

    @Autowired
    private HoutaiMapper houtaiMapper;

    @Autowired
    private UserMapper userMapper;


    public SummaryVo querySummary() {


        List<User> users = userMapper.selectList(null);

        SummaryVo summaryVo = new SummaryVo();
        summaryVo.setCumulativeUsers(users.size());

        //查询七天内活跃用户
        //获取登入时间
        long today = System.currentTimeMillis();
        int i1 = houtaiApi.query7UserList(today);
        summaryVo.setActivePassWeek(i1);

        //查询30天内活跃用户
        int i = houtaiApi.queryUserList(today);
        summaryVo.setActivePassMonth(i);

        //新增用户
        Date nowday = new Date();
        //获取今日零点时间


        TimeZone curTimeZone = TimeZone.getTimeZone("GMT+8");
        Calendar c = Calendar.getInstance(curTimeZone);
        c.setTime(nowday);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        long newday = c.getTime().getTime();
        Long yesterday = newday - 24 * 60 * 60 * 1000;
        Long oldyesday=yesterday- 24 * 60 * 60 * 1000;
        List todayList = new ArrayList();
        List yesterdayList = new ArrayList();

        //需要查看创建时间
        List<User> userList = userMapper.selectList(null);
        for (User user : userList) {
            if (user.getCreated().getTime() > newday) {
                //今日用户
                todayList.add(user);
            } else if (user.getCreated().getTime() > yesterday && user.getCreated().getTime() < newday) {
                yesterdayList.add(user);
            }

        }

        summaryVo.setNewUsersToday(todayList.size());

        //今日新增用户环比
        double newUsersTodayRate = 0;
        if (todayList.size() != 0 && yesterdayList.size() != 0) {
            newUsersTodayRate = ((double)todayList.size() / yesterdayList.size() - 1) * 100;
        } else if ((todayList.size() == 0) && (yesterdayList.size() != 0)) {
            newUsersTodayRate = (double)yesterdayList.size() * 100*(-1);
        } else if ((todayList.size() != 0) && (yesterdayList.size() == 0)) {
            newUsersTodayRate =(double) todayList.size() * 100;
        }

        summaryVo.setNewUsersTodayRate((int)newUsersTodayRate);

        //获取表数据
        List<Logs> logs = this.houtaiApi.queryLoginList();
        //登入次数


        List todayLoginList = new ArrayList<>();
        List yesterdayLoginList = new ArrayList<>();
        for (Logs log : logs) {
            if (log.getLoginTime() > newday) {
                todayLoginList.add(log.getLoginTime());
            }else  if (log.getLoginTime() > yesterday &&log.getLoginTime() < newday){
                yesterdayLoginList.add(log.getLoginTime());
            }
        }
        summaryVo.setLoginTimesToday(todayLoginList.size());

        //今日登陆次数环比
        double loginTimesTodayRate = 0D;
        if (todayLoginList.size() != 0 && yesterdayLoginList.size() != 0) {
            loginTimesTodayRate = ((double)todayLoginList.size() / yesterdayLoginList.size() - 1) * 100;
        } else if ((todayLoginList.size() == 0) && (yesterdayLoginList.size() != 0)) {
            loginTimesTodayRate = (double)yesterdayLoginList.size() * 100*(-1);
        } else if ((todayLoginList.size() != 0) && (yesterdayLoginList.size() == 0)) {
            loginTimesTodayRate = (double)todayLoginList.size() * 100;
        }

        summaryVo.setLoginTimesTodayRate((int)loginTimesTodayRate);

        //今日登陆用户
        int todayloginUser=this.houtaiApi.queryTodayLoginList(newday);

        int yesterdayloginUser=this.houtaiApi.queryYesterdayLoginList(yesterday);

         int lodyesdayUser =this.houtaiApi.queryOldyesDayLoginList(oldyesday);
        summaryVo.setActiveUsersToday(todayloginUser);
        summaryVo.setActiveUsersYesterday(yesterdayloginUser);
        //今日活跃用户环比
        double loginuserTodayRate = 0;
        if (todayloginUser != 0 && yesterdayloginUser != 0) {
            loginuserTodayRate = ((double)todayloginUser /yesterdayloginUser - 1) * 100;
        } else if ((todayloginUser == 0) && (yesterdayloginUser != 0)) {
            loginuserTodayRate =(double) yesterdayloginUser * 100*(-1);
        } else if ((todayloginUser != 0) && (yesterdayloginUser == 0)) {
            loginuserTodayRate =(double) todayloginUser * 100;
        }
        summaryVo.setActiveUsersTodayRate((int)loginuserTodayRate);
        //昨日活跃用户环比
        double loginuserYesterdayRate = 0;
        if (yesterdayloginUser != 0 && lodyesdayUser != 0) {
            loginuserYesterdayRate = ((double)yesterdayloginUser /lodyesdayUser - 1) * 100;
        } else if ((yesterdayloginUser == 0) && (lodyesdayUser != 0)) {
            loginuserYesterdayRate =(double) lodyesdayUser * 100*(-1);
        } else if ((yesterdayloginUser != 0) && (lodyesdayUser == 0)) {
            loginuserYesterdayRate =(double) yesterdayloginUser * 100;
        }
        summaryVo.setActiveUsersYesterdayRate((int)loginuserYesterdayRate);


        //昨日活跃用户
        return summaryVo;


    }
}
